from django.db import models

from utils.models import BaseModel


class server(BaseModel):
    """
    性能测试服务器表
    """

    name = models.CharField(max_length=50, help_text='服务器名称', verbose_name='服务器名称')
    host_ip = models.CharField(max_length=50, help_text='服务器ip', verbose_name='服务器ip')
    host_port = models.IntegerField(verbose_name='端口号', help_text='端口号', null=True, blank=True)
    sys_user_name = models.CharField(max_length=50, help_text='用户名', verbose_name='用户名')
    sys_user_passwd = models.CharField(max_length=128, help_text='密码', verbose_name='密码')
    default_code = models.BooleanField(verbose_name='是否设置为默认服务器', default=False)
    project = models.ForeignKey('projects.Project', on_delete=models.CASCADE, help_text='项目id', verbose_name='项目id',
                                related_name='server')


    def __str__(self):
        return self.name

    class Meta:
        db_table = 'performance_server'
        verbose_name = "性能测试服务器表"
        verbose_name_plural = verbose_name



class PerformanceTask(BaseModel):
    """
    性能测试任务表
    """
    choices_taskType = [('10', '普通任务'),('20', '定时任务')]
    choices_patternMode = [('10', '并发模式'), ('20', '阶梯模式')]
    choices_status = [('1', '执行中'), ('0', '执行完成'), ('99', '执行失败')]

    project = models.ForeignKey('projects.Project', on_delete=models.CASCADE, help_text='项目id', verbose_name='项目id',
                                related_name='perfTask')
    taskType = models.CharField(max_length=50, help_text='任务类型', verbose_name='任务类型', choices=choices_taskType)
    desc = models.CharField(max_length=200, help_text='任务描述', verbose_name='任务描述', blank=True, null=True)
    taskName = models.CharField(max_length=50, help_text='任务名称', verbose_name='任务名称')
    runPattern = models.CharField(max_length=50, help_text='运行模式', verbose_name='运行模式', choices=choices_patternMode, blank=True, null=True)
    status = models.CharField(max_length=50, help_text='运行状态', verbose_name='运行状态', choices=choices_status, blank=True, null=True)


    def __str__(self):
        return self.taskName

    class Meta:
        db_table = 'performance_task'
        verbose_name = "性能任务表"
        verbose_name_plural = verbose_name


class presetting(BaseModel):
    """
    性能测试预置配置表
    """
    choices_taskType = [('10', '普通任务'),('20', '定时任务')]
    choices_logMode = [('0', '关闭'),('10', '开启-全部日志'),('20', '开启-仅成功日志'),('30', '开启-仅失败日志')]
    choices_pressureMode = [('10', '并发模式'),('20', '阶梯模式')]
    choices_timeUnit = [('s', '秒'),('m', '分'),('h', '时')]
    choices_control = [('10', '集合模式'),('20', '单独模式')]
    choices_resource = [('10', '默认'),('20', '自定义')]
    choices_timeType = [('10', '固定'), ('20', '随机')]

    project = models.ForeignKey('projects.Project', on_delete=models.CASCADE, help_text='项目id', verbose_name='项目id',
                                related_name='presetting')
    serverArray = models.ManyToManyField(server,help_text='服务器id', verbose_name='服务器id', blank=True)
    task = models.OneToOneField(PerformanceTask, on_delete=models.SET_NULL, null=True, help_text='性能任务id', verbose_name='性能任务id',
                                related_name='presetting')
    name = models.CharField(max_length=128, help_text='预置配置名称', verbose_name='预置配置名称')
    rule = models.CharField(help_text='定时执行规则', verbose_name='定时任务', max_length=80, default='* * * * *')
    taskType = models.CharField(max_length=50, help_text='任务类型', verbose_name='任务类型', choices=choices_taskType)
    logMode = models.CharField(max_length=50, help_text='日志模式', verbose_name='日志模式', choices=choices_logMode)
    pressureMode = models.CharField(max_length=50, help_text='压测模式', verbose_name='压测模式', choices=choices_pressureMode)
    timeUnit = models.CharField(max_length=50, help_text='时间单位', verbose_name='时间单位', choices=choices_timeUnit)
    control = models.CharField(max_length=50, help_text='控制模式', verbose_name='控制模式', choices=choices_control)
    resource =models.CharField(max_length=50, help_text='机器选择方式', verbose_name='机器选择方式', choices=choices_resource)
    pressureConfig = models.JSONField(help_text='压测配置', verbose_name='压测配置', default=dict)
    isSetting = models.BooleanField(help_text='区分任务配置和预配置', verbose_name='区分预配置和任务配置', default=False)
    thinkTime = models.JSONField(help_text='思考时间', verbose_name='思考时间', default=[], null=True, blank=True)
    thinkTimeType = models.CharField(max_length=50, help_text='思考时间类型', verbose_name='思考时间类型', choices=choices_timeType)
    def __str__(self):
        return self.name

    class Meta:
        db_table = 'performance_presetting'
        verbose_name = "性能预配置表"
        verbose_name_plural = verbose_name



class TaskScence(models.Model):
    """
    性能测试场景表
    """
    task = models.ForeignKey(PerformanceTask, on_delete=models.CASCADE, help_text='性能任务id', verbose_name='性能任务id',
                                related_name='taskScence')
    env = models.ManyToManyField('projects.TestEnv', help_text='环境id', verbose_name='环境id',
                               related_name='taskScence', null=True, blank=True)
    name = models.CharField(max_length=100, verbose_name='场景名称', help_text='场景名称')
    weight = models.IntegerField(verbose_name='权重', help_text='权重', default=1)
    create_time = models.DateTimeField(verbose_name='创建时间', help_text='创建时间', auto_now_add=True, null=True, blank=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'performance_TaskScence'
        verbose_name = "性能场景表"
        verbose_name_plural = verbose_name


class TaskStep(BaseModel):
    """
    性能测试步骤表
    """
    scence = models.ForeignKey(TaskScence, on_delete=models.CASCADE, help_text='性能场景id', verbose_name='性能场景id')
    name = models.CharField(max_length=50, help_text='步骤控制器名称', verbose_name='步骤控制器名称')
    dlg = models.BooleanField(default=False, help_text='循环控制器是否展开', verbose_name='循环控制器是否展开')
    inputDlg = models.BooleanField(default=False, help_text='自定义脚本是否展开', verbose_name='自定义脚本是否展开')
    type = models.CharField(max_length=50, help_text='步骤控制器类型', verbose_name='步骤控制器类型')
    content = models.JSONField(help_text='步骤控制器内容', verbose_name='步骤控制器内容', default=dict, blank=True)
    script = models.TextField(help_text='步骤控制器脚本', verbose_name='步骤控制器脚本', default='', null=True, blank=True)
    desc = models.CharField('步骤控制器描述', max_length=200, help_text='步骤控制器描述', null=True, blank=True)
    status = models.BooleanField(default=True, help_text='是否启用', verbose_name='是否启用')
    debugResult = models.BooleanField(default=True, help_text='是否正常', verbose_name='是否正常')
    weight = models.IntegerField(null=True, blank=True, help_text='权重', verbose_name='权重')

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'performance_TaskStep'
        verbose_name = "性能步骤表"
        verbose_name_plural = verbose_name

class TaskScenceStep(BaseModel):
    """
    性能测试场景步骤表
    """
    step = models.ForeignKey(TaskStep, on_delete=models.CASCADE, help_text='性能步骤id', verbose_name='性能步骤id')
    task = models.ForeignKey(PerformanceTask, on_delete=models.CASCADE, help_text='性能任务id', verbose_name='性能任务id')
    scence = models.ForeignKey(TaskScence, on_delete=models.CASCADE, help_text='性能场景id', verbose_name='性能场景id')
    sort = models.IntegerField(help_text='执行顺序', verbose_name='执行顺序', blank=True)
    parent = models.ForeignKey('self', null=True, blank=True, related_name='children', on_delete=models.CASCADE)

    def __str__(self):
        return str(self.id)

    class Meta:
        db_table = 'performance_taskScenceStep'
        verbose_name = "性能任务场景步骤表"
        verbose_name_plural = verbose_name


# class TaskReport(BaseModel):
#     """
#     性能测试报告表
#     """
#     choices_status = [('1', '报告-执行中'), ('0', '报告-已完成'), ('99', '报告-运行失败')]
#
#     task = models.ForeignKey(PerformanceTask, on_delete=models.CASCADE, help_text='性能任务id', verbose_name='性能任务id') # TODO:任务名称、任务模式、压测模式从该外键取得
#     reportName = models.CharField(max_length=50, help_text='报告名称', verbose_name='报告名称')
#     desc = models.CharField(max_length=200, help_text='报告描述', verbose_name='报告描述', blank=True, null=True)
#     reportStatus = models.CharField(max_length=50, help_text='运行状态', verbose_name='运行状态', choices=choices_status, blank=True, null=True)
#     avgTps = models.FloatField(help_text='平均tps', verbose_name='平均tps', blank=True, null=True)
#     avgCpu = models.FloatField(help_text='平均cpu', verbose_name='平均cpu', blank=True, null=True)
#     avgMemory = models.FloatField(help_text='平均内存', verbose_name='平均内存', blank=True, null=True)
#     reportResult = models.TextField(help_text='报告分析', verbose_name='报告分析', blank=True, null=True)
#     resultAnalyse = models.TextField(help_text='报告分析', verbose_name='报告分析', blank=True, null=True)






